package DSMini;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;



public class isFourMAES {
    private static final int FILE_SIZE =16777216;
    private static final int Siza=125829120;
    public static Boolean discriminator(byte[] testTable) {
        // 文件数量
        int numFiles = 256;
        //线性池的大小
        int numThreads = 4;

        // Create a thread pool with the same number of threads as files
        ExecutorService executorService = Executors.newFixedThreadPool(numThreads);

        // List to hold Future objects
        List<Future<Boolean>> futures = new ArrayList<>();

        // Create and submit tasks to the executor service
        for (int K3 = 0; K3 < numFiles; K3++) {
            final int fileIndex = K3;
            futures.add(executorService.submit(() -> processFile(fileIndex,testTable)));
        }

        // Monitor the futures and cancel all tasks if one completes successfully
        try {
            for (Future<Boolean> future : futures) {
                if (future.get()) {
                    // A task has completed successfully
                    System.out.println("A condition was met. Cancelling all tasks...");
                    for (Future<Boolean> f : futures) {
                        f.cancel(true);
                    }
                    return true;
                }
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            // Shutdown the executor service
            executorService.shutdown();

        }
        return false;
    }

    private static Boolean processFile(int K3,byte[] testTable) {
        String fileName = "table_" + K3 + ".dat";
        try (DataInputStream dis = new DataInputStream(new FileInputStream(fileName))) {
            byte[] table = new byte[Siza]; // Adjust the size as per your requirements
            dis.readFully(table);

            // Implement your logic here
            // Return true if the condition is met, otherwise false
            boolean conditionMet = checkCondition(table,K3,testTable);
            if (conditionMet) {
                System.out.println("找到了匹配表"+"table_" + K3 + ".dat");
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static boolean checkCondition(byte[] table,int k3,byte[] testTable) {
        //这为一个4轮的结果
        //byte[] fourcry={0b100,0b101,0b111,0b0,0b1010,0b1011,0b1000,0b111,0b110,0b101,0b10,0b1100,0b1,0b1010,0b10};0 0 0 0
        //byte[] fourcry={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//无
        //byte[] fourcry={0b0,0b101,0b111,0b100,0b1010,0b1111,0b1000,0b1001,0b1000,0b101,0b1000,0b1010,0b1011,0b1100,0b110};
        // Implement your condition checking logic here
        // Return true if the condition is met
        // For example:
        for (int i = 0; i < FILE_SIZE; i++) {
            for (int j = 0; j < 15; j++) {
                int index = (int) (i *7.5 + (double) j/2);
                byte temp=table[index];
                if ((i&1)==0){
                    //当一组表里的索引下表0--15为偶数时
                    if ((j&1)==0){
                        temp=(byte) (temp>>4&0b00001111);
                    }else {
                        temp= (byte) (temp&0b00001111);
                    }
                }else {
                    //当一组表里的索引下表0--15为偶数时
                    if ((j&1)==0){
                        temp= (byte) (temp&0b00001111);
                    }else {
                        temp=(byte) (temp>>4&0b00001111);
                    }
                }
                if (temp!=testTable[j]){
                    break;
                }
                if (j==14){
                    //System.out.println("");
                    return true;
                }
            }
        }
        //System.out.println(k3+"文件无匹配对");
        return false;
    }
}
